<?php

namespace App\Console\Commands\model;

use App\Dep\ModelDep;
use App\Dep\TextToImageCategorysDep;
use App\Dep\TextToImageDep;
use App\Enum\TextToImageEnum;
use App\Lib\AigcSdk;
use App\Lib\ModelSdk;
use Faker\Provider\Text;
use Illuminate\Console\Command;

class TitleCmd extends Command
{
    //打标
    public $signature = 'model:text-to-title';

    public function handle()
    {
        $dep = new TextToImageDep();
        $resWait = $dep->getWaitTitle();
        if ($resWait->isEmpty()) {
            $this->log('have no wait title');
            return false;
        }

        $aiSdk = new AigcSdk();
        $modelDep = new ModelDep();
        $json = $this->json();
        $resWait->each(function ($item) use ($dep, $aiSdk,$json,$modelDep) {
            try {
                $resModel = $modelDep->first($item->lora_id);
                $resChat = $aiSdk->chat("根据我提供的信息帮我返回文案的标题和描述存储到json里，json结构是{$json}，要求标题14字以内，描述40字以内，请严格按照我要求的json格式", "我想写一篇关于{$resModel->name}的文案，主题是{$item->theme}");
                $origin= $resChat['output']['choices'][0]['message']['content'];
                $this->log("chat res {$origin}");
                $content = str_replace(['```'], '', $origin);
                $contentArr = json_decode($content, true);
                if(!isset($contentArr['title']) || !isset($contentArr['description'])){
                    $data = [
                        'status' => TextToImageEnum::STATUS_TITLE_ERR,
                        'title_ok_at' => date('Y-m-d H:i:s'),
                        'title_origin' => $origin
                    ];
                    $dep->edit($item->id,$data);
                    $this->log('title error',['id'=>$item->id]);

                    $this->log("content error");;
                    return true;
                }
                $this->log("chat res arr",$contentArr);
                $data = [
                    'status' => TextToImageEnum::STATUS_TITLE_SUCCESS,
                    'title' => $this->clearTitle($contentArr['title']),
                    'description' => $this->clearDescription($contentArr['description']),
                    'title_ok_at' => date('Y-m-d H:i:s'),
                    'title_origin' => $origin
                ];
                $dep->edit($item->id,$data);
                $this->log('title ok',['id'=>$item->id]);
            }catch (\Exception $e) {
                $this->log("error {$e->getMessage()}");
            }
        });
    }

    private function log($msg, $context = [])
    {
        $this->info("text to image log: {$msg} " . json_encode($context, JSON_UNESCAPED_UNICODE));
        log_daily("text-to-title")->info($msg, $context);
    }

    private function json()
    {
        return file_get_contents(config_path('/title.json'));
    }

    private function clearTitle($title){
        $res = str_replace("标题：","",$title);
        return $res;
    }

    private function clearDescription($description){
        $res = str_replace("描述：","",$description);
        return $res;
    }

}
